/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.risk.hid.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.risk.hid.dto.RiskHidSuperviseDTO;
import com.hopes.bizz.risk.hid.entity.RiskHidSuperviseEntity;
import com.hopes.bizz.risk.hid.service.RiskHidSuperviseService;
import com.hopes.bizz.risk.hid.vo.RiskHidSuperviseVo;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 隐患督办
 *
 * @author lyb
 * @date 2024-02-22 15:34:47
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/riskHidSupervise")
@Tag(description = "riskHidSupervise", name = "隐患督办管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskHidSuperviseController {

    private final RiskHidSuperviseService riskHidSuperviseService;

    /**
     * 分页查询
     *
     * @param page             分页对象
     * @param riskHidSupervise 隐患督办
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('hid_riskHidSupervise_view')")
    public R getRiskHidSupervisePage(Page page, RiskHidSuperviseEntity riskHidSupervise) {
        Integer superviseStatus = riskHidSupervise.getSuperviseStatus();
        Long orgId = riskHidSupervise.getOrgId();
        LambdaQueryWrapper<RiskHidSuperviseEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Objects.nonNull(orgId), RiskHidSuperviseEntity::getOrgId, orgId)
                .eq(Objects.nonNull(superviseStatus), RiskHidSuperviseEntity::getSuperviseStatus, superviseStatus);
        return R.ok(riskHidSuperviseService.page(page, wrapper).convert(entity -> BeanUtil.copyProperties(entity, RiskHidSuperviseVo.class)));
    }


    /**
     * 通过id查询隐患督办
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('hid_riskHidSupervise_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(riskHidSuperviseService.getById(id));
    }

    /**
     * 新增隐患督办
     *
     * @param riskHidSupervise 隐患督办
     * @return R
     */
    @Operation(summary = "新增隐患督办", description = "新增隐患督办")
    @SysLog("新增隐患督办")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('hid_riskHidSupervise_add')")
    public R save(@RequestBody RiskHidSuperviseEntity riskHidSupervise) {
        return R.ok(riskHidSuperviseService.save(riskHidSupervise));
    }

    /**
     * 修改隐患督办
     *
     * @param riskHidSupervise 隐患督办
     * @return R
     */
    @Operation(summary = "修改隐患督办", description = "修改隐患督办")
    @SysLog("修改隐患督办")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('hid_riskHidSupervise_edit')")
    public R updateById(@RequestBody RiskHidSuperviseEntity riskHidSupervise) {
        return R.ok(riskHidSuperviseService.updateById(riskHidSupervise));
    }

    /**
     * 通过id删除隐患督办
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除隐患督办", description = "通过id删除隐患督办")
    @SysLog("通过id删除隐患督办")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('hid_riskHidSupervise_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(riskHidSuperviseService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param riskHidSupervise 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('hid_riskHidSupervise_export')")
    public List<RiskHidSuperviseEntity> export(RiskHidSuperviseEntity riskHidSupervise) {
        return riskHidSuperviseService.list(Wrappers.query(riskHidSupervise));
    }

    /**
     * @Description 隐患督办
     * @Author lyb
     * @Date 2024/2/22
     **/
    @PostMapping("supervise")
    public R supervise(@Validated @RequestBody RiskHidSuperviseDTO riskHidSuperviseDTO) {
        riskHidSuperviseService.supervise(riskHidSuperviseDTO);
        return R.ok();
    }
}